package com.jxdinfo.hussar.cloud.module.example;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jxdinfo.hussar.authorization.enums.UserStatus;
import com.jxdinfo.hussar.authorization.permit.model.SysUsers;
//import com.jxdinfo.hussar.authorization.permit.service.ISysUsersService;
import com.jxdinfo.hussar.base.cloud.service.SysAuthUserDetailsService;
import com.jxdinfo.hussar.common.exception.BaseException;
import com.jxdinfo.hussar.common.security.BaseSecurityUtil;
import com.jxdinfo.hussar.common.security.SecurityUser;
import com.jxdinfo.hussar.core.util.ToolUtil;
import com.jxdinfo.hussar.platform.core.crypto.credential.AbstractCredentialsMatcher;
import com.jxdinfo.hussar.platform.core.support.service.AuthSecurityUserDetailService;
import com.jxdinfo.hussar.platform.core.support.service.dto.LoginUser;
import com.jxdinfo.hussar.platform.core.support.service.dto.UserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 认证登录获取登录用户信息的接口实现类
 *
 * @author jianghuaizhi
 * @since 2021-11-27
 */
@Service
public class AuthSecurityUserDetailServiceImpl implements AuthSecurityUserDetailService {

    @Autowired
    private SysAuthUserDetailsService sysAuthUserDetailsService;
    @Resource
    private AbstractCredentialsMatcher credentialsMatcher;
//    @Resource
//    private ISysUsersService iSysUsersService;

    /**
     * 根据账号密码获取登录用户信息
     *
     * @param loginUser 用户登录实体
     * @return
     */
    @Override
    public UserDetails loadUserByUsername(LoginUser loginUser) {
        return sysAuthUserDetailsService.getLoginUserByAccount(loginUser);
    }

    /**
     * 扩展第三方登录
     *
     * @param code
     * @return
     */
    @Override
    public UserDetails loadUserBySocial(String code) {
        return null;
    }

    /**
     * 二次认证时校验用户密码是否正确
     */
    @Override
    public Boolean checkUserPassword(String password) {
        if (ToolUtil.isEmpty(password)) {
            throw new BaseException("二次认证异常，输入密码信息为空");
        }
        SecurityUser securityUser = BaseSecurityUtil.getUser();
        if (ToolUtil.isEmpty(securityUser)) {
            throw new BaseException("二次认证异常，获取用户信息失败");
        }
//        SysUsers sysUsers = iSysUsersService.getOne(new QueryWrapper<SysUsers>().lambda()
//////                .eq(SysUsers::getUserAccount, securityUser.getAccount())
//////                .ne(SysUsers::getAccountStatus, UserStatus.DELETE.getCode()).ne(SysUsers::getAccountStatus, UserStatus.CANCEL.getCode()), false);
//////        if (ToolUtil.isEmpty(sysUsers)) {
//////            throw new BaseException("账号为: " + securityUser.getAccount() + " 的用户不存在");
//////        }
//////        String encPwd = credentialsMatcher.passwordEncode(password.getBytes());
//////        return encPwd.equalsIgnoreCase(sysUsers.getPassword());
        return true;
    }
}
